/**
 * 
 */
package com.obradovicnjegovan.webprodavnica.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.obradovicnjegovan.webprodavnica.controller.ControllerAL;
import com.obradovicnjegovan.webprodavnica.entities.Narudzbenica;
import com.obradovicnjegovan.webprodavnica.entities.StatusNarudzbenice;

/**
 * @author njegovan
 *
 */
public class Narudzbenice {
	@Inject
	private ControllerAL controllerAL;
	@Property
	private Narudzbenica narudzbenica;
	@Inject
    private AlertManager alertManager;
	
	public List<Narudzbenica> getNarudzbenice(){
		List<Narudzbenica> lista = new ArrayList<Narudzbenica>();
		for (Narudzbenica narudzbenica : controllerAL.pronadjiSveNarudzbenice()) {
			if (narudzbenica.getKorisnik()!=null) {
				lista.add(narudzbenica);
			}
		}
		return lista;
	}
	@CommitAfter
	public void onPromeniStatus(Narudzbenica n){
		switch (n.getStatusNarudzbenice()) {
		case naCekanju:
			n.setStatusNarudzbenice(StatusNarudzbenice.placena);
			alertManager.info(controllerAL.sacuvajNarudzbenicu(n));
			break;
		case placena:
			n.setStatusNarudzbenice(StatusNarudzbenice.ponistena);
			alertManager.info(controllerAL.sacuvajNarudzbenicu(n));
			break;
		case ponistena:
			n.setStatusNarudzbenice(StatusNarudzbenice.naCekanju);
			alertManager.info(controllerAL.sacuvajNarudzbenicu(n));
			break;
		default:
			break;
		}
		
	}
	@InjectPage
	private ProizvodiNarudzbenice proizvodiNarudzbenice;
	
	public Object onPrikaziProizvode(Narudzbenica narudzbenica){
		proizvodiNarudzbenice.setId(narudzbenica.getKorpa().get_id());
		return proizvodiNarudzbenice;
	}
}